package com.skyline.jfs.base.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.skyline.jfs.base.model.Administrator;
import com.skyline.jfs.base.service.AdminAdvanService;
import com.skyline.jfs.base.service.AdminBasicService;

@Controller
@RequestMapping(value = "/admin/")
public class AdminAdvanController {
	@Autowired
	private AdminAdvanService adminAdvanService;

	@Autowired
	private AdminBasicService adminBasicService;

	@RequestMapping(value = "addAdmin.html", method = RequestMethod.GET)
	public String addAdministratorRequest(HttpSession session) {
		if (session.getAttribute("administrator") != null) {
			return "admin/addAdmin";
		} else {
			return "admin/login";
		}
	}

	@RequestMapping(value = "addAdmin.html", method = RequestMethod.POST)
	public String addAdministratorExecute(Long jobNum, String firstName,
			String lastName, String department, String position, String phone,
			String email, String password, String passwordConfirm,
			HttpSession session) {
		if (session.getAttribute("administrator") != null) {
			adminAdvanService.add(jobNum, firstName, lastName, department,
					position, phone, email, password, passwordConfirm);
			return "redirect:/admin/clusterAdmins.html";
		} else {
			return "admin/login";
		}
	}

	@RequestMapping(value = "modifyAdmin/{id}.html", method = RequestMethod.GET)
	public String modifyAdministratorRequest(HttpSession session,
			@PathVariable Integer id, HttpServletRequest requst) {
		if (session.getAttribute("administrator") != null) {
			Administrator a = adminBasicService.getById(id);
			requst.setAttribute("administrator", a);
			return "admin/modifyAdmin";
		} else {
			return "admin/login";
		}
	}

	@RequestMapping(value = "modifyAdmin.html", method = RequestMethod.POST)
	public String modifyAdministratorExecute(Integer id,Long jobNum, String firstName,
			String lastName, String department, String position, String phone,
			String email, String password, String passwordConfirm,
			HttpSession session) {
		if (session.getAttribute("administrator") != null) {
			adminAdvanService.modify(id,jobNum, firstName, lastName, department,
					position, phone, email, password, passwordConfirm);
			return "redirect:/admin/clusterAdmins.html";
		} else {
			return "admin/login";
		}
	}
	
	@RequestMapping(value = "deleteAdmin/{id}.html", method = RequestMethod.GET)
	public String delteAdministrator(HttpSession session,
			@PathVariable Integer id) {
		if (session.getAttribute("administrator") != null) {
			adminAdvanService.delete(id);
			return "redirect:/admin/clusterAdmins.html";
		} else {
			return "admin/login";
		}
	}
}
